package com.pactera.miyuangroup.filter;

import com.pactera.miyuangroup.vo.response.ResultModel;
import com.pactera.miyuangroup.vo.response.ResultStatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理
 *
 * @author Wangwu
 * @create 2018-04-02 上午11:49
 **/
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultModel errorHandler(HttpServletRequest request, Exception e) throws Exception {
        log.info("Exception:", e);
        return ResultModel.fail(ResultStatusCode.BUSINESS_ERROR);
    }

    @ExceptionHandler(value = AccessDeniedException.class)
    @ResponseBody
    public ResultModel AccessDeniedHandler(HttpServletRequest request, Exception e) throws Exception {
        return ResultModel.fail(ResultStatusCode.INVALID_TOKEN);
    }

    @ExceptionHandler(value = UsernameNotFoundException.class)
    @ResponseBody
    public ResultModel UsernameNotFoundHandler(HttpServletRequest request, Exception e) throws Exception {
        return ResultModel.fail(ResultStatusCode.INVALID_PASSWORD);
    }

}
